import blue
import trinity
import spaceObject
import turret
import nodemanager
import timecurves
import base
import util
import uthread
import random
import log
entityExplosionsS = ['res:/Emitter/tracerexplosion/NPCDeathS1.blue',
 'res:/Emitter/tracerexplosion/NPCDeathS3.blue',
 'res:/Emitter/tracerexplosion/NPCDeathS4.blue']
entityExplosionsM = ['res:/Emitter/tracerexplosion/NPCDeathM1.blue',
 'res:/Emitter/tracerexplosion/NPCDeathM3.blue',
 'res:/Emitter/tracerexplosion/NPCDeathM4.blue']
entityExplosionsL = ['res:/Emitter/tracerexplosion/NPCDeathL1.blue',
 'res:/Emitter/tracerexplosion/NPCDeathL3.blue',
 'res:/Emitter/tracerexplosion/NPCDeathL4.blue']

class StructureSentryGun(spaceObject.PlayerOwnedStructure):
    __guid__ = 'spaceObject.StructureSentryGun'

    def __init__(self):
        spaceObject.PlayerOwnedStructure.__init__(self)
        self.targets = []
        self.modules = {}
        self.turretPair = None
        self.fitted = False



    def Assemble(self):
        slimItem = sm.StartService('michelle').GetBallpark().GetInvItem(self.id)
        godmaStateManager = sm.StartService('godma').GetStateManager()
        godmaType = godmaStateManager.GetType(slimItem.typeID)
        self.gfxTurretID = godmaType.gfxTurretID
        self.typeID = slimItem.typeID
        if (settings.user.ui.Get('turretsEnabled', 1) and self.IsAnchored()):
            self.FitHardpoints()



    def FitHardpoints(self):
        if self.fitted:
            return 
        self.fitted = True
        if turret.TurretSet.Winter2010TurretsEnabled_ToBeRemoved():
            turretSet = turret.TurretSet()
            turretSet.Initialize(util.GraphicFile(self.gfxTurretID), 1)
            self.model.turretSets.append(turretSet.GetTurretSet())
            self.modules[self.id] = turretSet
            self.model.RebuildTurretPositions()
            return 
        turretPair = turret.Turret_sc2()
        eveTurretPair = turretPair.Initialize(util.GraphicFile(self.gfxTurretID), 'locator_turret_1a', 'locator_turret_1b')
        self.model.turrets.append(eveTurretPair)
        self.turretPair = turretPair
        self.modules[self.id] = turretPair
        self.model.RebuildCachedLocatorIndices()



    def SetBuiltStructureGraphics(self, animate = 0):
        spaceObject.PlayerOwnedStructure.SetBuiltStructureGraphics(self, animate)
        if settings.user.ui.Get('turretsEnabled', 1):
            self.FitHardpoints()



    def Release(self):
        if (self.turretPair is not None):
            self.turretPair.Release()
        spaceObject.PlayerOwnedStructure.Release(self, 'spaceObject.StructureSentryGun')



    def Explode(self):
        if not settings.user.ui.Get('explosionEffectsEnabled', 1):
            return self.exploded
        else:
            if (self.radius < 100.0):
                explosionURL = entityExplosionsS[(self.typeID % 3)]
            elif (self.radius < 400.0):
                explosionURL = entityExplosionsM[(self.typeID % 3)]
            elif (self.radius <= 900.0):
                explosionURL = entityExplosionsL[(self.typeID % 3)]
            if (self.radius <= 900.0):
                return spaceObject.SpaceObject.Explode(self, explosionURL)
            if self.exploded:
                return False
            self.exploded = True
            exlosionBasePath = 'res:/Emitter/tracerexplosion/'
            if (self.radius > 3000.0):
                extraPath = 'StructureDeathRadius1500.blue'
            elif (self.radius > 1500.0):
                extraPath = 'StructureDeathRadius1000.blue'
            else:
                extraPath = 'StructureDeathRadius500.blue'
            explosionURL = (exlosionBasePath + extraPath)
            gfx = trinity.Load(explosionURL.replace('.blue', '.red'))
            if (gfx is None):
                return False
            explodingObjectDisplay = [ x for x in gfx.curveSets if (x.name == 'ExplodingObjectDisplay') ]
            if (gfx.__bluetype__ != 'trinity.EveRootTransform'):
                root = trinity.EveRootTransform()
                root.children.append(gfx)
                root.name = explosionURL
                gfx = root
            self.model.translationCurve = self
            self.model.rotationCurve = None
            gfx.translationCurve = self
            self.explosionModel = gfx
            scene2 = sm.StartService('sceneManager').GetRegisteredScene2('default')
            scene2.objects.append(gfx)
            if len(explodingObjectDisplay):
                explodingObjectDisplay = explodingObjectDisplay[0]
                explodingObjectDisplay.bindings[0].destinationObject = self.model
                self.explosionDisplayBinding = explodingObjectDisplay.bindings[0]
            return True



exports = {'spaceObject.StructureSentryGun': StructureSentryGun}

